package com.cc2019.buke.user.Activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cc2019.buke.R;
import com.cc2019.buke.user.tools.Constants;
import com.cc2019.buke.user.tools.DataBaseHelper;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener{
    Button button;

    SQLiteDatabase sqLiteDatabase;
    DataBaseHelper helper;

    private EditText id;
    private EditText username;
    private EditText password;
    private EditText emails;
    private String name_str;
    private String paswd_str;
    private String email_str;
    private Button register;
    private String id_str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        helper=new DataBaseHelper(this,"user.db",null,2);
        username=(EditText) findViewById(R.id.register_name);
        password=(EditText)findViewById(R.id.register_password);
        emails=(EditText)findViewById(R.id.register_email);
        register=(Button) findViewById(R.id.register);
        id=(EditText)findViewById(R.id.register_id);

        sqLiteDatabase=helper.getWritableDatabase();

        //设置监听按钮
        register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //通过id分配按钮事件
        switch (v.getId()) {
            //注册按钮
            case R.id.register:
                new AlertDialog.Builder(RegisterActivity.this).setTitle("系统提示")
                        .setMessage("是否确认提交？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                id_str=id.getText().toString();
                                name_str = username.getText().toString();
                                paswd_str = password.getText().toString();
                                email_str=emails.getText().toString();

                                //给数据库设置游标
                                Cursor cursor = sqLiteDatabase.query(Constants.TABLE_NAME, new String[]{"name"}, "name=?", new String[]{name_str}, null, null, null);
                                if (cursor.getCount() != 0) {
                                    Toast.makeText(RegisterActivity.this, "该用户已注册!", Toast.LENGTH_SHORT).show();
                                } else if(!TextUtils.isEmpty(id_str)&&!TextUtils.isEmpty(name_str)&&!TextUtils.isEmpty(paswd_str)&&!TextUtils.isEmpty(email_str)){
                                    helper.insert(sqLiteDatabase,id_str, name_str, paswd_str,email_str);
                                    Toast.makeText(RegisterActivity.this, "注册成功,请登录!", Toast.LENGTH_SHORT).show();
                                    Intent intent=new Intent(RegisterActivity.this,LoginActivity.class);
                                    startActivity(intent);
                                } else if(TextUtils.isEmpty(id_str)&&!TextUtils.isEmpty(name_str)&&!TextUtils.isEmpty(paswd_str)&&!TextUtils.isEmpty(email_str)){
                                    Toast.makeText(RegisterActivity.this,"ID不能为空哦~",Toast.LENGTH_SHORT).show();
                                }else if(!TextUtils.isEmpty(id_str)&&TextUtils.isEmpty(name_str)&&!TextUtils.isEmpty(paswd_str)&&!TextUtils.isEmpty(email_str)){
                                    Toast.makeText(RegisterActivity.this,"姓名不能为空哦~",Toast.LENGTH_SHORT).show();
                                }else if(!TextUtils.isEmpty(id_str)&&!TextUtils.isEmpty(name_str)&&TextUtils.isEmpty(paswd_str)&&!TextUtils.isEmpty(email_str)){
                                    Toast.makeText(RegisterActivity.this,"密码不能为空哦~",Toast.LENGTH_SHORT).show();
                                }else if(!TextUtils.isEmpty(id_str)&&!TextUtils.isEmpty(name_str)&&!TextUtils.isEmpty(paswd_str)&&TextUtils.isEmpty(email_str)){
                                    Toast.makeText(RegisterActivity.this,"邮箱不能为空哦~",Toast.LENGTH_SHORT).show();
                                }else {
                                    Toast.makeText(RegisterActivity.this,"请检查信息是否已经填写完毕",Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).setNegativeButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).show();
                break;
        }
    }
}